const rateLimit = require('express-rate-limit');
const config = require('../config/config');

const createRateLimiter = (options = {}) => {
  return rateLimit({
    windowMs: options.windowMs || config.rateLimit.windowMs,
    max: options.max || config.rateLimit.max,
    message: options.message || config.rateLimit.message,
    standardHeaders: true,
    legacyHeaders: false,
    handler: (req, res) => {
      res.status(429).json({
        error: 'Too many requests',
        message: options.message || config.rateLimit.message,
        retryAfter: Math.ceil(options.windowMs / 1000) || Math.ceil(config.rateLimit.windowMs / 1000)
      });
    }
  });
};

const apiLimiter = createRateLimiter();

const strictLimiter = createRateLimiter({
  windowMs: 60000,
  max: 5,
  message: 'Too many generation requests, please try again later.'
});

module.exports = {
  createRateLimiter,
  apiLimiter,
  strictLimiter
};